/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.edu.itschapala.ssrp.dao;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import mx.edu.itschapala.ssrp.modelo.Usuario;

/**
 *
 * @author pello
 */
@Stateless
public class UsuarioDAO extends AbstractDAO<Usuario> {

    @PersistenceContext(unitName = "SSRP-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public Usuario getPorNcontrol(String username) {
        Query q = em.createNamedQuery("Usuario.findByNcontrol");
        q.setParameter("ncontrol", username);
        try {
            Usuario u = (Usuario) q.getSingleResult();
            return u;
        } catch (Exception e) {
            return null;
        }
    }

    public UsuarioDAO() {
        super(Usuario.class);
    }

    public List<Usuario> getPorTipo(String tipo) {
        Query q = em.createNamedQuery("Usuario.findByPerfil");
        q.setParameter("perfil", tipo);
        try {
            List<Usuario> u = q.getResultList();
            return u;
        } catch (Exception e) {
            return new ArrayList<>();
        }
    }

}
